package util;

import java.sql.*;

public class DriverManegerUitl {
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 链接数据库的url
     */
    private static final String url = "jdbc:mysql://localhost:3306/stumansys?characterEncoding=utf-8";
    /**
     * 链接数据库的账号
     */
    private static final String username = "root";
    /**
     * 链接数据库的密码
     */
    private static final String password = "root";
    private static Connection conn ;

    /**
     * 获取数据库会话链接
     */
    public static Connection getConnection()  {
        try{
            conn = DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }

    public static void closed(Connection conn, PreparedStatement pre, ResultSet resultSet){
        if(null != conn){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(null != pre){
            try {
                pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(null != resultSet){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
